using Shipeng.HRMS.Domain.ContentManage;
using Shipeng.Util;
using Shipeng.Util.DataBase;
using SqlSugar;

namespace Shipeng.HRMS.Service.ContentManage
{
    /// <summary>
    /// 创 建：李仕鹏
    /// 日 期：2022-08-27 23:36
    /// 描 述：新闻管理服务类
    /// </summary>
    public class ArticleNewsService : DataFilterService<ArticleNewsEntity>, IDenpendency
    {
        public ArticleNewsService(IUnitOfWork unitOfWork) : base(unitOfWork)
        {

        }

        #region 获取数据
        public async Task<List<ArticleNewsEntity>> GetList(string keyword = "")
        {
            var query = repository.IQueryable();
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_Title.Contains(keyword) || a.F_Tags.Contains(keyword));
            }
            return await query.Where(a => a.F_DeleteMark == false).OrderBy(a => a.F_Id, OrderByType.Desc).ToListAsync();
        }

        public async Task<List<ArticleNewsEntity>> GetLookList(SoulPage<ArticleNewsEntity> pagination, string keyword = "", string CategoryId = "")
        {
            //反格式化显示只能用"等于"，其他不支持
            Dictionary<string, Dictionary<string, string>> dic = new Dictionary<string, Dictionary<string, string>>();
            Dictionary<string, string> enabledTemp = new Dictionary<string, string>();
            enabledTemp.Add("1", "有效");
            enabledTemp.Add("0", "无效");
            dic.Add("F_EnabledMark", enabledTemp);
            Dictionary<string, string> isTrue = new Dictionary<string, string>();
            isTrue.Add("1", "是");
            isTrue.Add("0", "否");
            dic.Add("F_IsTop", isTrue);
            dic.Add("F_IsHot", isTrue);
            pagination = ChangeSoulData(dic, pagination);
            //获取新闻列表
            var query = repository.Db.Queryable<ArticleNewsEntity, ArticleCategoryEntity>((a, b) => new JoinQueryInfos(
                JoinType.Left, a.F_CategoryId == b.F_Id && b.F_EnabledMark == true
                ))
            .Select((a, b) => new ArticleNewsEntity
            {
                F_Id = a.F_Id.SelectAll(),
                F_CategoryName = b.F_FullName,
            }).MergeTable();
            if (!string.IsNullOrEmpty(keyword))
            {
                query = query.Where(a => a.F_Title.Contains(keyword));
            }
            if (!string.IsNullOrEmpty(CategoryId))
            {
                query = query.Where(a => a.F_CategoryId.Contains(CategoryId));
            }
            query = query.Where(a => a.F_DeleteMark == false);
            //权限过滤
            query = GetDataPrivilege<ArticleNewsEntity>("a", "", query);
            return await query.ToPageListAsync(pagination);
        }
        /// <summary>
        /// 获取新闻详情
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        public async Task<ArticleNewsEntity> GetForm(string keyValue)
        {
            var query = repository.Db.Queryable<ArticleNewsEntity, ArticleCategoryEntity>((a, b) => new JoinQueryInfos(
                JoinType.Left, a.F_CategoryId == b.F_Id && b.F_EnabledMark == true
                ))
            .Select((a, b) => new ArticleNewsEntity
            {
                F_Id = a.F_Id.SelectAll(),
                F_CategoryName = b.F_FullName,
            }).MergeTable();
            if (!string.IsNullOrEmpty(keyValue))
            {
                query = query.Where(a => a.F_Id == keyValue);
            }
            //字段权限处理
            return GetFieldsFilterData(await query.FirstAsync());
        }
        #endregion

        #region 提交数据
        public async Task SubmitForm(ArticleNewsEntity entity, string keyValue)
        {
            if (string.IsNullOrEmpty(entity.F_Zhaiyao))
            {
                entity.F_Zhaiyao = TextHelper.GetSubString(WebHelper.NoHtml(entity.F_Description), 255);
            }
            if (string.IsNullOrEmpty(entity.F_SeoTitle))
            {
                entity.F_SeoTitle = entity.F_Title;
            }
            if (string.IsNullOrEmpty(entity.F_SeoKeywords))
            {
                entity.F_SeoKeywords = entity.F_Zhaiyao;
            }
            if (string.IsNullOrEmpty(entity.F_SeoDescription))
            {
                entity.F_SeoDescription = entity.F_Zhaiyao;
            }

            if (string.IsNullOrEmpty(keyValue))
            {
                entity.F_DeleteMark = false;
                //此处需修改
                entity.Create();
                await repository.Insert(entity);
            }
            else
            {
                //此处需修改
                entity.Modify(keyValue);
                await repository.Update(entity);
            }
        }

        public async Task DeleteForm(string keyValue)
        {
            var ids = keyValue.Split(',');
            await repository.Delete(a => ids.Contains(a.F_Id));
        }
        #endregion

    }
}
